package com.framework.bdf4j.sysadm.controller;

import java.util.List;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.framework.bdf4j.sysadm.entity.SysUser;
import com.framework.bdf4j.sysadm.service.SysIdepermRoleService;
import com.framework.bdf4j.sysadm.service.SysUserService;
import com.framework.bdf4j.sysadm.vo.IdeUserVO;

import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

/**
 * IDE权限分配Controller
 * 
 * @author cfss
 * @date 2020-07-17
 */
@Controller
@AllArgsConstructor
@RequestMapping("/admin/ideperm")
public class SysIdepermRoleController {

	   private final SysIdepermRoleService ideRolePermService;
	    private final SysUserService userService;
	    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

	    @ApiOperation(value = "ide用户登录", notes = "IDE用户登录")
	    @GetMapping("/authUser/{userName}/{password}")
	    @ResponseBody
	    public String authUser(@PathVariable String userName, @PathVariable String password) {
	        IdeUserVO vo = new IdeUserVO();
	        vo.setLogin("false");
	        SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getLoginname, userName));
	        if (user == null) {
	            vo.setErrorMsg("用户不存在");
	            vo.setResultCode("9999");
	            return JSONObject.toJSONString(vo);
	        }
	        if (!ENCODER.matches(password,user.getPassword())) {
	            vo.setErrorMsg("密码不正确");
	            vo.setResultCode("9999");
	            return JSONObject.toJSONString(vo);
	        }
	        if ("1".equals(user.getDelFlag()) || "1".equals(user.getLockFlag())) {
	            vo.setErrorMsg("用户已禁用");
	            vo.setResultCode("9999");
	            return JSONObject.toJSONString(vo);
	        }
	        List<String> permlist = ideRolePermService.selectPermCodeByUserId(user.getUserId());
	        vo.setLogin("true");
	        vo.setResultCode("0000");
	        vo.setToken("4");
	        // vo.setExpiredTime(DateUtil.formatDate(user.getExpiredTime()));
	        vo.setAuths(permlist);
	        return JSONObject.toJSONString(vo);
	    }

}
